from rest_framework import serializers
from .models import *
from management.models import DepartmentModel,PositionModel
from staff.models import StaffModel
import re
from django.forms import ValidationError


# 流程模板序列化器
class FlowTemplateSer(serializers.ModelSerializer):
    class Meta:
        model = FlowTemplateModel
        fields = [
            "id",
            "name",
        ]


# 流程审批人序列化器
class NodeSer(serializers.ModelSerializer):
    class Meta:
        model = NodeModel
        fields = [
            "id",
            "flow_id",
            "staff_id",
        ]

    def validate(self, attrs):
        flow_id = attrs.get("flow_id")
        staff_id = attrs.get("staff_id")

        flow_list = FlowModel.objects.filter(is_delete=False).values("id")
        flow_list = [i.get("id") for i in flow_list]
        if flow_id not in flow_list:
            raise ValidationError(f"{flow_id} 该流程不存在！")

        staff_list = StaffModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if staff_id not in staff_list:
            raise ValidationError(f"{staff_id} 该员工不存在！")

        return attrs


# 节点必填选项表序列化器
class NodeNeedSer(serializers.ModelSerializer):
    class Meta:
        model = NodeNeedModel
        fields = [
            "id",
            "flow_template_id",
            "name",
            "type_id",
        ]


# 流程模板表序列化器
class FlowSer(serializers.ModelSerializer):
    class Meta:
        model = FlowModel
        fields = [
            "id",
            "name",
            "is_use",
            "flow_tamplate_id",
            "desc",
        ]


    def validate(self, attrs):
        name = attrs.get("name")
        is_use = attrs.get("is_use")
        flow_tamplate_id = attrs.get("flow_tamplate_id")
        desc = attrs.get("desc")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 节点名称由2到30位汉字、数字、字母或下划线组成！')

        if not isinstance(is_use,bool):
            raise ValidationError(f'{is_use} 是否启用类型错误！')

        flow_template_list = FlowTemplateModel.objects.filter(is_delete=False).values("id")
        flow_template_list = [i.get("id") for i in flow_template_list]
        if flow_tamplate_id not in flow_template_list:
            raise ValidationError(f"{flow_tamplate_id} 该模板不存在！")

        if desc != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$",desc):
                raise ValidationError(f'{name} 简介由2到256位汉字、数字、字母或下划线组成！')

        return attrs


# 节点必填项类型表序列化器
class NeedTypeSer(serializers.ModelSerializer):
    class Meta:
        model = NeedTypeModel
        fields = "__all__"


# 节点必填项选项表序列化器
class NeedOptionSer(serializers.ModelSerializer):
    class Meta:
        model = NeedOptionModel
        fields = [
            "id",
            "name",
            "need_id",
        ]


# 流程节点实例表序列化器
class NewNodeSer(serializers.ModelSerializer):
    class Meta:
        model = NewNodeModel
        fields = [
            "id",
            "new_flow_id",
            "status",
            "staff_id",
            "remarks",
        ]

    def validate(self, attrs):
        new_flow_id = attrs.get("new_flow_id")
        status = attrs.get("status")
        staff_id = attrs.get("staff_id")
        remarks = attrs.get("remarks")

        new_flow_list = NewFlowModel.objects.filter(is_delete=False).values("id")
        new_flow_list = [i.get("id") for i in new_flow_list]
        if new_flow_id not in new_flow_list:
            raise ValidationError(f"{new_flow_id} 该实例流程不存在！")

        if status != None:
            if status < 0 or status > 5:
                raise ValidationError(f"{status} 节点状态不存在！")

        staff_list = StaffModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if staff_id not in staff_list:
            raise ValidationError(f"{staff_id} 该员工不存在！")

        if remarks != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$", remarks):
                raise ValidationError(f'{remarks} 备注允许2到256位汉字、数字、字母或下划线组成！')

        return attrs


# 节点信息表序列化器
class NewNodeNeedSer(serializers.ModelSerializer):
    class Meta:
        model = NewNodeNeedModel
        fields = [
            "id",
            "new_flow_id",
            "need_id",
            "need",
        ]


# 流程实例表序列化器
class NewFlowSer(serializers.ModelSerializer):
    class Meta:
        model = NewFlowModel
        fields = [
            "id",
            "staff_id",
            "flow_id",
            "status",
            "end_date",
        ]

    def validate(self, attrs):
        staff_id = attrs.get("staff_id")
        flow_id = attrs.get("flow_id")
        status = attrs.get("status")

        staff_list = StaffModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if staff_id not in staff_list:
            raise ValidationError(f"{staff_id} 该员工不存在！")

        flow_list = FlowModel.objects.filter(is_delete=False).values("id")
        flow_list = [i.get("id") for i in flow_list]
        if flow_id not in flow_list:
            raise ValidationError(f"{flow_id} 该流程不存在！")

        if status != None:
            if status < 0 or status > 5:
                raise ValidationError(f"{status} 流程状态不存在！")

        return attrs

